package com.mytest.listener;

import com.mytest.pojo.ProductType;
import com.mytest.service.productTypeService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import java.util.List;

/**
 * @BelongsProject: java-85-course-code
 * @BelongsPackage: com.mytest.listener
 * @Author: LiTongMiao
 * @Date: 2022/2/7 15:39
 * @Description: TODO
 */
@WebListener
public class ProductTypeListenter implements ServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        //手工从Spring容器中取出productTyserviceImpl的对象
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext_*.xml");
        productTypeService productTypeService=(productTypeService)context.getBean("ProductTypeServiceImpl");
        List<ProductType> typeList=productTypeService.getAll();
        //放入全局应用的作用域，供新增界面，修改界面，前台查询功能提供全部商品类别集合
        sce.getServletContext().setAttribute("typeList",typeList);
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {

    }
}
